package com.qimai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.net.URLEncoder;

@Data
public abstract class BaseParam {
    private String analysis;

    public void setAnalysis(String path){
        this.analysis = QiMaiEnc.getAnalysis(values(), path);
    }

    public String param(){
        return genParam(this);
    }

    public abstract String[] values();

    public static String genParam(Object obj){
        JSONObject json = JSON.parseObject(JSON.toJSONString(obj));
        boolean first = true;
        StringBuilder ret = new StringBuilder();
        for (String s : json.keySet()) {
            if(first){
                first = false;
            }else {
                ret.append("&");
            }
            ret.append(s).append("=").append(URLEncoder.encode(json.getString(s)));
        }
        return ret.toString();
    }

}